<?php
namespace app\user\logic\user;

use app\common\exception\LogicException;
use app\user\model\user\Follow as FollowModel;

/**
* 粉丝管理
*/
class Follow extends FollowModel
{

    // 关注列表
    public function getFollowList($param)
    {
        $user_id = $param['user_id']??request()->mid;
        $list    = $this
            ->with(['follow_user_info',])
            ->where('user_id', $user_id)
            ->field('follow_user_id as user_id')
            ->paginate($this->getListRows())
            ->each(function($i) use ($user_id) {
                $i->has_follow_me = $this->whereUserId($i->user_id)->whereFollowUserId($user_id)->count();
            })
        ;
        return $list;
    }

    // 关注用户
    public function follow($param)
    {
        // 验证数据
        $this->getValidate()->scene('follow')->failException()->check($param);
        // 检查已关注
        $followed = $this
            ->withTrashed()
            ->scope('mid')
            ->where('follow_user_id', $param['user_id'])
            ->findOrEmpty();
                    
        if (!$followed->isEmpty() && !$followed->delete_time) throw new LogicException('已关注');
        // 检查取消关注
        if ($followed->delete_time) {
            $followed->restore();
            unset($followed->delete_time);
            return $followed;
        }
        // 关注
        $re = $this::create([
            'user_id'        => request()->mid,
            'follow_user_id' => $param['user_id']
        ]);
        if (!$re) throw new LogicException('关注失败');

        return $re;
    }

    // 取关用户
    public function unfollow($param)
    {
        // 验证数据
        $this->getValidate()->scene('follow')->failException()->check($param);
        // 检查已关注
        $unfollow = $this
            ->scope('mid')
            ->where('follow_user_id', $param['user_id'])
            ->findOrFail();

        $re = $unfollow->delete();
        if (!$re) throw new LogicException('取关失败');

        return $re;
    }

    // 关注列表
    public function getFollower($param)
    {
        $user_id = $param['user_id']??request()->mid;
        return $this
        ->with(['follow_user_info'])
        ->field('user_id')
        ->where('follow_user_id', $user_id)
        ->paginate($this->getListRows())
        ->each(function($i){
            $i->is_follow  = $this->scope('mid')->where('follow_user_id', $i->user_id)->count();
        });
    }

    // 获取关注人数
    public function getFollowNum($user_id)
    {
        $user_id = $user_id ? : request()->mid;
        return $this->where('user_id', $user_id)->count();
    }

    // 获取粉丝人数
    public function getFollowerNum($user_id)
    {
        $user_id = $user_id ? : request()->mid;
        return $this->where('follow_user_id', $user_id)->count();
    }

    // 当前用户是否关注指定用户
    public function isFollow($follow_user_id)
    {
        if ( $mid = request()->mid ) {
            $follow = $this->where('follow_user_id', $follow_user_id)->where('user_id', $mid)->findOrEmpty();
            return $follow->isEmpty() ? 0 : 1;
        }
        return 0;
    }
}
